//
//  ScenicTableViewCtrl.m
//  LvXingPai
//
//  Created by _Junxee on 16/8/13.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "ScenicTableViewCtrl.h"
#import "ScenicTableViewCell.h"
#import "scenicModel.h"

@interface ScenicTableViewCtrl ()
@property(nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation ScenicTableViewCtrl

- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

-(void)setScenModel:(DestinModel *)ScenModel{
    _ScenModel = ScenModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setBarButStyle];
    self.title = [NSString stringWithFormat:@"%@景点",_ScenModel.zhName];
}

-(void)setBarButStyle{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_icon_navigation_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"master_search"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.rightBarButtonItem = rightBtn;
}

-(void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)loadData{
    
    AFHTTPRequestOperationManager * Manger = [AFHTTPRequestOperationManager manager];
    Manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [Manger GET:[NSString stringWithFormat:@"http://api.lvxingpai.com/app/poi/viewspots?imgWidth=300&locality=%@&page=0&pageSize=15",_ScenModel.pushId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dict[@"result"];
        for (NSDictionary *dict2 in arr) {
            scenicModel * sModel = [[scenicModel alloc]init];
            sModel.zhName = dict2[@"zhName"];
            sModel.timeCostDesc = dict2[@"timeCostDesc"];
            NSArray * arr2 = dict2[@"images"];
            if (arr2.count != 0) {
                NSDictionary * dict3 = arr2[0];
                sModel.url = dict3[@"url"];
            }
            
            [self.dataArr addObject:sModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    scenicModel *model = self.dataArr[indexPath.row];
    cell.topLabel.text = model.zhName;
    cell.botLabel.text = [NSString stringWithFormat:@"建议游玩%@",model.timeCostDesc];
    [cell.leftImageV sd_setImageWithURL:[NSURL URLWithString:model.url]];

    return cell;
}



@end
